//
//
#include "Stdafx.h"
#include "Algorithm3D.h"


namespace Geometric
{
	bool Algorithm3D::isVisible(Point3D& _view, Point3D& _A, Point3D& _B, Point3D& _C, Point3D& _inside)
	{
		Point3D center;
		Vector3D V, N, tmp;
		double result = 0;

		// get the center of triangle ABC
		center.m_x = (_A.m_x + _B.m_x + _C.m_x) / 3;
		center.m_y = (_A.m_y + _B.m_y + _C.m_y) / 3;
		center.m_z = (_A.m_z + _B.m_z + _C.m_z) / 3;

		// get the vertical vector of triangle ABC
		// there are two vectors, get the one which point to the outside of the tetrahedron
		getVertical(_A, _B, _C, N);
		// judge if it needs reverse according to the dot result
		tmp.set(_inside, center);
		if(Gmath::isLess(tmp.getDot(N), 0))
		{
			N.m_x = -N.m_x;
			N.m_y = -N.m_y;
			N.m_z = -N.m_z;
		}

		// get the vector(start:view, end:center)
		V.set(_view, center);

		// get dot result
		result = V.getDot(N);

		// is visible
		return (Gmath::isLess(result, 0)) ? true : false;
	}
}

